package com.woniuxy.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.CustomExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * 延迟队列配置类
 */
@Configuration
public class DelayConfiguration {
    /**
     * 队列
     * @return
     */
    @Bean
    public Queue orderQueue() {
        return new Queue("order_queue");
    }

    /**
     * 交换机
     * @return
     */
    @Bean
    public CustomExchange customExchange() {
        Map<String, Object> map = new HashMap();
        map.put("x-delayed-type", "direct");
        //名称，类型，是否持久化，是否自动删除
        return new CustomExchange("delay_direct_exchange", "x-delayed-message", true, false, map);
    }

    @Bean
    public Binding bindOrderToExchange(Queue orderQueue, CustomExchange customExchange) {

        return BindingBuilder.bind(orderQueue).to(customExchange).with("orders").noargs();
    }
}
